<?php
//  This file is part of EkkinCMS.
//
//  EkkinCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  EkkinCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with EkkinCMS.  If not, see <http://www.gnu.org/licenses/>.

$root_path = "../";
include("../includes/utils.php");
#$name = $_POST["img"];
#print_r($_POST);
#echo $name;

$name = basename($_FILES["file"]["name"]);
$upload_type = $_POST["upload"];
if ($upload_type == 'image') {
    $imdir = $_POST["imdir"];
    $f = random_file(15);
    $sys = explode(".", $name);
    $n = count($sys)-1;
    $f_ext = $sys[$n];
    $f = "$f.$f_ext";
    $target_path = "../$imdir/$f";
    $thumb_path = "../$imdir/thumbs/$f";
    while (file_exists($target_path)){
        $f = random_file(15);
        $target_path = "../$imdir/$f";
    }
    $ext = array(".png", ".jpg", ".gif", ".JPG");
} elseif ($upload_type == 'file'){
    $fdir = $_POST["fdir"];
    $f = $name;
    $target_path = "$fdir/$f";
    $ext = array(".pdf", ".doc", ".odt", ".docx", ".ods", ".pptx", ".ppt", ".flv");
}


$valid = 0;
foreach ($ext as $e){
    if (str_endswith( $name, $e ) ){
        $valid = 1;
    }
}

if ($valid){
    $src = $_FILES["file"]["tmp_name"];

    if (move_uploaded_file($src, "$target_path")){
        echo "upload succeded $name => $target_path";
        if ($upload_type = 'image'){
            create_thumb($target_path, $thumb_path);
        }
    }

}
?>
